package com.atguigu.lease.web.admin.config;

import com.atguigu.lease.common.converter.StringToBaseEnumConverter;
import com.atguigu.lease.common.converter.StringToItemTypeConverter;
import com.atguigu.lease.web.admin.interceptor.AuthenticationInterceptor;
import com.atguigu.lease.web.admin.properties.AuthPathProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableConfigurationProperties(value = AuthPathProperties.class)
@SpringBootConfiguration
public class MyWebMvcConfig implements WebMvcConfigurer {

//    @Autowired
//    private StringToItemTypeConverter stringToItemTypeConverter;
//
//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        //注册转换器
//        registry.addConverter(stringToItemTypeConverter);
//    }

    @Autowired
    private StringToBaseEnumConverter stringToBaseEnumConverter;

    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Autowired
    private AuthPathProperties authPathProperties;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        //注册转换器工厂
        registry.addConverterFactory(stringToBaseEnumConverter);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns(authPathProperties.getInclude()) // 配置拦截的路径
                .excludePathPatterns(authPathProperties.getExclude()); // 配置不拦截的路径
    }
}
